/**
 * Created by zeopean on 2015/10/9.
 */

$(function(){
    /** 初始化ajax  */
    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    /** 保存编辑结果 **/
    $(document).on('click' , '.save' , function(){
        var userId  = $('#user_id').val();
        var url     = EditAdminById + userId;
        $.ajax({
            type:"post",
            url:url,
            dataType:"json",
            data:$('#form-admin').serialize(),
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });
    /** end **/

    /** 编辑操作 页面显示 **/
    $(document).on('click' , '.edit-admin' , function(){
        var userId = $(this).attr('data');
        $('#user_id').val(userId);

        /** 通过userId 获取信息 */
        $.ajax({
            type: "get",
            url: GetAdminById,
            dataType: "json",
            data: {
                'userId'    : userId
            },
            success: function (obj) {
                if (obj.level === "success") {
                    $('input[name=username]').val(obj.data.username);
                    $('input[name=email]').val(obj.data.email);
                    $('input[name=phone]').val(obj.data.phone);
                    $('input[name=password]').val('********');
                }
            }
        });
    });
    /** end **/

    /** 删除操作  **/
    $(document).on('click' ,'.del-admin' , function(){
        var userId  = $(this).attr('data');
        var tmp     = $(this);
        $.ajax({
            type: "get",
            url: DelAdminById,
            dataType: "json",
            data: {
                'userId'    : userId
            },
            success: function (obj) {
                if (obj.level === "success") {
                    noty({text:obj.message,type:obj.level,timeout:500});
                    tmp.parents('tr').hide();
                }
            },
            error: function (obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });

    /** end */


    $(document).on('click' , '.admin' , function(){
        location.reload();
    });

});
